package com.syas.web.controllers;

import com.syas.common.utilities.ExcelUtils;
import com.syas.common.utilities.PageListUtil;
import com.syas.model.models.FmgerCalResult;
import com.syas.service.interfaces.IGradeService;
import com.syas.service.interfaces.ISalcalService;
import com.syas.web.auth.AuthPassport;
import com.syas.web.models.FmgerSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/salcal")
public class SalcalController extends BaseController {
	@Autowired
    @Qualifier("SalcalService")
    private ISalcalService salcalService;
    @Autowired
    @Qualifier("GradeService")
    private IGradeService gradeService;
    //excel的sheet名称
    private  static final  String SHEETNAME = "Sheet1";
    //excel的总列数
    private  static final  int COLCNT = 28;
    @AuthPassport
	@RequestMapping(value="/list", method = {RequestMethod.GET})
    public String list(HttpServletRequest request, Model model, FmgerSearchModel searchModel){
        model.addAttribute("requestUrl", request.getServletPath());
        model.addAttribute("requestQuery", request.getQueryString());
        model.addAttribute("searchModel", searchModel);
        //岗位列表
        model.addAttribute("stationList", gradeService.getGradeList());
        //核算结果列表
        int pageNo = ServletRequestUtils.getIntParameter(request, PageListUtil.PAGE_NO_NAME, PageListUtil.DEFAULT_PAGE_NO);
        int pageSize = ServletRequestUtils.getIntParameter(request, PageListUtil.PAGE_SIZE_NAME, PageListUtil.DEFAULT_PAGE_SIZE);
        model.addAttribute("contentModel", salcalService.listPage(
                searchModel.getEzubouname(),
                searchModel.getCertid(),
                pageNo,
                pageSize));
        return "salcal/list";
    }
    @AuthPassport
    @RequestMapping(value="/list", method = {RequestMethod.POST})
    public void toExcel(HttpServletRequest request,HttpServletResponse response, Model model, FmgerSearchModel searchModel){
        try {
            List<FmgerCalResult> resultList = salcalService.listPage(searchModel.getEzubouname(),
                    searchModel.getCertid());
            LinkedHashMap<String,String> hashMap = new LinkedHashMap<String,String>();
            toHashMap(hashMap);
            ExcelUtils.listToExcel(resultList,hashMap,SHEETNAME,response);
            model.addAttribute("result","导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("result","导出失败");
        }
    }

    /**
     * @param hashMap ：excel表头和字段属性名字的hashmap集合
     * @return
     * @MethodName : toHashMap
     * @Description : 审核结果对应
     */
    private void toHashMap(LinkedHashMap<String,String> hashMap){
        hashMap.put("certId","身份证号码");
        hashMap.put("ezboUname","e租宝账号");
        hashMap.put("city","城市");
        hashMap.put("cityGrade","城市级别");
        hashMap.put("departmentLevel1","一级部门");
        hashMap.put("departmentLevel2","二级部门");
        hashMap.put("departmentLevel3","三级部门");
        hashMap.put("departmentLevel4","四级部门");
        hashMap.put("departmentLevel5","五级部门");
        hashMap.put("departmentLevel6","六级部门");
        hashMap.put("station","岗位");
        hashMap.put("grade","等级");
        hashMap.put("directLeaderName","直接领导");
        hashMap.put("directLeaderEzbouname","直接领导e租宝账号");
        hashMap.put("protection","是否保护期");
        hashMap.put("buildPeriod","是否筹建期");
        hashMap.put("perperf","个人业绩");
        hashMap.put("perdeductrate","个人提成比率");
        hashMap.put("manageperf","管理业绩");
        hashMap.put("archstaffnum","标准架构编制");
        hashMap.put("archstaffnum","实际团队编制");
        hashMap.put("standperf","岗位标准考核业绩");
        hashMap.put("avgtargetperf","人均业绩目标");
        hashMap.put("targetperf","岗位实际考核目标");
        hashMap.put("kaidanrate","开单率");
        hashMap.put("shuhuirate","赎回率");
        hashMap.put("jixsalybase","绩效工资基数");
        hashMap.put("targetreachrate","目标达成率");
        hashMap.put("managedeductrate","管理提成比率");
        hashMap.put("managejixcore","管理绩效考核分数");
        hashMap.put("actattendaynum","实际出勤天数");
        hashMap.put("perdeduct","个人提成");
        hashMap.put("managededuct","管理提成");
        hashMap.put("deduct","提成");
        hashMap.put("jixsaly","绩效工资");
        hashMap.put("caltime","计算时间");
    }
}
